package com.nengchuan.eic.nceconomy.controller;

import com.nengchuan.eic.nceconomy.entity.dto.ExcelUploadDTO;
import com.nengchuan.eic.nceconomy.entity.dto.VerifyConfirmDTO;
import com.nengchuan.eic.nceconomy.entity.vo.VerifyResultVO;
import com.nengchuan.eic.nceconomy.service.VerifyResultService;
import com.nengchuan.eic.nceconomy.util.IdUtils;
import com.nengchuan.eic.nceconomy.util.PoiUtils;
import com.nengchuan.model.model.WrappedResult;
import com.nengchuan.util.NullCheckUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 功能描述:
 *
 * @Author: liurui
 * @Date: 2020/5/8 0008 上午 11:24
 */
@RestController
@Slf4j
@Api(value = "VerifyResultController", tags = "验证结果模块")
public class VerifyResultController {

    @Autowired
    private VerifyResultService verifyResultService;


    @ApiOperation("验证结果导出")
    @PostMapping("/download_verify_result")
    @ApiResponse(code = 200, message = "成功为true,失败为false", response = WrappedResult.class)
    public void downloadVerifyResult(@RequestBody ExcelUploadDTO dto, HttpServletResponse response){
        List<VerifyResultVO.ContentResultExcel> list = verifyResultService.getOnlineView(dto).stream().map(VerifyResultVO.ContentResultExcel::new).collect(Collectors.toList());
        String fileName=IdUtils.getFormatUUID()+".xlsx";
        PoiUtils.writeExcel(response,list,fileName,VerifyResultVO.ContentResultExcel.class);
    }



    @ApiOperation("验证结果查询")
    @PostMapping(value = "/verify_result",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponse(code = 200, message = "成功为true,失败为false", response = WrappedResult.class)
    public WrappedResult<VerifyResultVO> getVerifyResult(@RequestBody ExcelUploadDTO dto){
        if(NullCheckUtils.isNull(dto.getPid())||NullCheckUtils.isNull(dto.getCategoryCode())||NullCheckUtils.isNull(dto.getProjectCode())||NullCheckUtils.isNull(dto.getVersion())){
            return WrappedResult.fail("参数不能为空");
        }
        return WrappedResult.success(verifyResultService.getVerifyResult(dto));
    }


    @ApiOperation("确认结果")
    @PostMapping(value = "/confirm_result",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponse(code = 200, message = "成功为true,失败为false", response = WrappedResult.class)
    public WrappedResult<String> getConfirmResult(@RequestBody VerifyConfirmDTO dto){
        if(NullCheckUtils.isNull(dto.getVerifyId())||NullCheckUtils.isNull(dto.getPid())||NullCheckUtils.isNull(dto.getCategoryCode())||NullCheckUtils.isNull(dto.getProjectCode())||NullCheckUtils.isNull(dto.getVersion())){
            return WrappedResult.fail("参数不能为空");
        }
        return WrappedResult.success(verifyResultService.getConfirmResult(dto));
    }

    @ApiOperation("在线预览-内容校验")
    @PostMapping(value = "/online_view",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponse(code = 200, message = "成功为true,失败为false", response = WrappedResult.class)
    public WrappedResult<List<Map<String,Object>>> getOnlineView(@RequestBody ExcelUploadDTO dto){
        if(NullCheckUtils.isNull(dto.getPid())||NullCheckUtils.isNull(dto.getCategoryCode())||NullCheckUtils.isNull(dto.getProjectCode())||NullCheckUtils.isNull(dto.getVersion())){
            return WrappedResult.fail("参数不能为空");
        }
        List<Map<String,Object>> list=new ArrayList<>();
        Map<String, List<VerifyResultVO.ContentResult>> collect = verifyResultService.getOnlineView(dto).stream().collect(Collectors.groupingBy(VerifyResultVO.ContentResult::getExcelName));
        for(String map:collect.keySet()){
            Map<String,Object> map1=new HashMap<>();
            map1.put("name",map);
            map1.put("content",collect.get(map));
            list.add(map1);
        }
        return WrappedResult.success(list);
    }


}
